package com.jshoperxms.controller.mall.backstage.basicuser;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.BasicUserT;
import com.jshoperxms.entity.BrandT;
import com.jshoperxms.entity.SysRoleT;
import com.jshoperxms.entity.SysUserRoleRpT;
import com.jshoperxms.service.BasicUserTService;
import com.jshoperxms.service.SysRoleTService;
import com.jshoperxms.service.SysUserRoleRpTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/11/1.
 * Des:系统用户角色
 */
@Controller
@RequestMapping("/mall/basicuserrole")
public class SysUserRoleRpTController extends BaseTController implements IBaseCtrl<SysUserRoleRpT> {

    @Resource
    private SysUserRoleRpTService sysUserRoleRpTService;

    @Resource
    private SysRoleTService sysRoleTService;

    @Resource
    private BasicUserTService basicUserTService;
    private Map<String, Object> responsedata = new HashMap<>();

    @Override
    public ResponseEntity save(@RequestBody SysUserRoleRpT rq) {
        return null;
    }

    //获取系统用户的角色信息
    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody SysUserRoleRpT rq) {
        if(StringUtils.isBlank(rq.getBasicuserid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            List<SysRoleT>beanlist=new ArrayList<>();
            Criterion criterion= Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState());
            criterion=Restrictions.and(criterion).add(Restrictions.eq("basicuserid",rq.getBasicuserid()));
            List<SysUserRoleRpT>list=this.sysUserRoleRpTService.findByCriteria(SysUserRoleRpT.class, criterion, Order.desc(TagWords.UPDATETIME));
            if(!list.isEmpty()){
                for(SysUserRoleRpT s:list){
                    SysRoleT bean=this.sysRoleTService.findByPK(SysRoleT.class,s.getSysroleid());
                    if(bean!=null){
                        beanlist.add(bean);
                    }
                }
            }
            //获取系统用户信息
            BasicUserT basicUserT=this.basicUserTService.findByPK(BasicUserT.class,rq.getBasicuserid());
            if(basicUserT!=null){
                responsedata.put(TagWords.BEAN,basicUserT);
            }
            responsedata.put(TagWords.BEANLIST,beanlist);
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity del(@RequestBody SysUserRoleRpT rq) {
        return null;
    }


    //更新系统用户权限信息
    @RequestMapping(value="/update",method= RequestMethod.POST)
    @Override
    public ResponseEntity update(@RequestBody SysUserRoleRpT rq) {
        if(StringUtils.isBlank(rq.getSysroleid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            this.sysUserRoleRpTService.updateSysUserRoleTransactional(rq.getBasicuserid(),rq.getSysroleid());
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG, TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        return null;
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {

    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {

    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {

    }

    @Override
    public void processPageList(List<SysUserRoleRpT> list) {

    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
